package qc.module.platform.mapper;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
import qc.module.platform.dto.messageConfig.MessageConfigDto;
import qc.module.platform.entity.QcMessageConfig;
import qc.common.core.enums.QcMessageEnumClass;

import java.util.List;

/**
 * MessageConfigMapper
 *
 * @author QuCheng Tech
 * @create 2023/11/03
 */
@Mapper
public interface MessageConfigMapper {

    MessageConfigMapper MAPPER = Mappers.getMapper(MessageConfigMapper.class);

    /**
     * dto -> entity
     */
    @Mapping(target = "msgCode", source = "code")
    @Mapping(target = "rcvType", ignore = true)
    @Mapping(target = "level", ignore = true)
    @Mapping(target = "timelyType", ignore = true)
    @Mapping(target = "noticeType", ignore = true)
    QcMessageConfig toEntity(MessageConfigDto dto);

    /**
     * entity -> dto
     */
    @Mapping(target = "code", source = "msgCode")
    @Mapping(target = "receiverType", source = "rcvType.index")
    @Mapping(target = "level", source = "level.index")
    @Mapping(target = "timelyType", source = "timelyType.index")
    @Mapping(target = "noticeType", source = "noticeType.index")
    MessageConfigDto toDto(QcMessageConfig en);

    /**
     * enList -> dtoList
     */
    List<MessageConfigDto> toDtoList(List<QcMessageConfig> enList);

    /**
     * 新增或修改dto -> entity
     */
    default QcMessageConfig addOrUpdateDtoToEntity(MessageConfigDto dto) {
        // 1.dto转为entity
        QcMessageConfig en = MessageConfigMapper.MAPPER.toEntity(dto);

        // 2.处理接收对象类型，非空时进行转换，空值时使用默认
        en.setRcvType(getReceiverTypeEnum(dto.getReceiverType()));

        // 3.处理重要等级，非空时进行转换，空值时使用默认
        en.setLevel(getLevelEnum(dto.getLevel()));

        // 4.处理时效性类型，非空时进行转换，空值时使用默认
        en.setTimelyType(getTimelyTypeEnum(dto.getTimelyType()));

        // 5.处理通知方式类型，非空时进行转换，空值时使用默认
        en.setNoticeType(getNoticeTypeEnum(dto.getNoticeType()));

        return en;
    }

    /**
     * 获取接收对象类型
     *
     * @param index
     * @return
     */
    static QcMessageEnumClass.rcvTypeEnum getReceiverTypeEnum(Integer index) {
        if (index != null && index > 0x0) {
            for (QcMessageEnumClass.rcvTypeEnum item : QcMessageEnumClass.rcvTypeEnum.values()) {
                if (item.getIndex() == index) {//判断序号使用getIndex，判断内容使用getName
                    return item;
                }
            }
        }
        return QcMessageEnumClass.rcvTypeEnum.ALL_USER;
    }

    /**
     * 获取重要等级
     *
     * @param index
     * @return
     */
    static QcMessageEnumClass.levelEnum getLevelEnum(Integer index) {
        if (index != null && index > 0x0) {
            for (QcMessageEnumClass.levelEnum item : QcMessageEnumClass.levelEnum.values()) {
                if (item.getIndex() == index) {//判断序号使用getIndex，判断内容使用getName
                    return item;
                }
            }
        }
        return QcMessageEnumClass.levelEnum.MEDIUM;
    }

    /**
     * 获取时效性类型
     *
     * @param index
     * @return
     */
    static QcMessageEnumClass.timelyTypeEnum getTimelyTypeEnum(Integer index) {
        if (index != null && index > 0x0) {
            for (QcMessageEnumClass.timelyTypeEnum item : QcMessageEnumClass.timelyTypeEnum.values()) {
                if (item.getIndex() == index) {//判断序号使用getIndex，判断内容使用getName
                    return item;
                }
            }
        }
        return null;
    }

    /**
     * 获取通知方式类型
     *
     * @param index
     * @return
     */
    static QcMessageEnumClass.noticeTypeEnum getNoticeTypeEnum(Integer index) {
        if (index != null && index > 0x0) {
            for (QcMessageEnumClass.noticeTypeEnum item : QcMessageEnumClass.noticeTypeEnum.values()) {
                if (item.getIndex() == index) {//判断序号使用getIndex，判断内容使用getName
                    return item;
                }
            }
        }
        return null;
    }

}
